package com.nervenets.general.utils;

import java.util.HashMap;

/**
 * 媒体类型工具类
 *
 * @author somewhere
 */
public class MimeTypeUtil {
    public static final String IMAGE_PNG = "image/png";

    public static final String IMAGE_JPEG = "image/jpeg";

    public static final String IMAGE_BMP = "image/bmp";

    public static final String IMAGE_GIF = "image/gif";

    public static final String[] IMAGE_EXTENSION = {"bmp", "gif", "jpg", "jpeg", "png"};

    public static final String[] FLASH_EXTENSION = {"swf", "flv"};

    public static final String[] MEDIA_EXTENSION = {"swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg",
            "asf", "rm", "rmvb"};

    public static final String[] DEFAULT_ALLOWED_EXTENSION = {
            // 图片
            "bmp", "gif", "jpg", "jpeg", "png",
            // word excel powerpoint
            "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
            // 压缩文件
            "rar", "zip", "gz", "bz2",
            // pdf
            "pdf",
            //apk
            "apk"};
    private final static HashMap<String, String> mimeTypes = new HashMap<String, String>() {{
        put("txt", "text/plain");
        put("html", "text/html");
        put("htm", "text/htm");
        put("png", "image/png");
        put("bmp", "application/x-bmp");
        put("ico", "image/x-icon");
        put("svg", "image/svg+xml");
        put("jpg", "image/jpeg");
        put("jpeg", "image/jpeg");
        put("doc", "application/msword");
        put("dot", "application/msword");
        put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        put("dotx", "application/vnd.openxmlformats-officedocument.wordprocessingml.template");
        put("xls", "application/vnd.ms-excel");
        put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        put("ppt", "application/vnd.ms-powerpoint");
        put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
        put("tar", "application/x-tar");
    }};

    public static String getExtension(String prefix) {
        switch (prefix) {
            case IMAGE_PNG:
                return "png";
            case IMAGE_JPEG:
                return "jpeg";
            case IMAGE_BMP:
                return "bmp";
            case IMAGE_GIF:
                return "gif";
            default:
                return "";
        }
    }

    public static String getMimeType(String extension) {
        return mimeTypes.getOrDefault(extension, "application/" + extension);
    }
}
